package com.d3works.apps.purchase.service;

import com.d3works.apps.base.db.Company;
import com.d3works.apps.base.db.Currency;
import com.d3works.apps.base.db.Partner;
import com.d3works.apps.base.db.PriceList;
import com.d3works.apps.base.db.TradingName;
import com.d3works.apps.purchase.db.PurchaseOrder;
import com.d3works.auth.db.User;
import com.d3works.exception.D3worksException;
import com.google.inject.persist.Transactional;
import java.time.LocalDate;
import java.util.List;

public interface PurchaseOrderService {

  PurchaseOrder _computePurchaseOrderLines(PurchaseOrder purchaseOrder) throws D3worksException;

  @Transactional(rollbackOn = {Exception.class})
  PurchaseOrder computePurchaseOrder(PurchaseOrder purchaseOrder) throws D3worksException;

  /**
   * Peupler une commande.
   *
   * <p>Cette fonction permet de déterminer les tva d'une commande à partir des lignes de factures
   * passées en paramètres.
   *
   * @param purchaseOrder
   * @throws D3worksException
   */
  void _populatePurchaseOrder(PurchaseOrder purchaseOrder) throws D3worksException;

  /**
   * Calculer le montant d'une commande.
   *
   * <p>Le calcul est basé sur les lignes de TVA préalablement créées.
   *
   * @param purchaseOrder
   * @throws D3worksException
   */
  void _computePurchaseOrder(PurchaseOrder purchaseOrder) throws D3worksException;

  /**
   * Permet de réinitialiser la liste des lignes de TVA
   *
   * @param purchaseOrder Une commande.
   */
  void initPurchaseOrderLineTax(PurchaseOrder purchaseOrder);

  PurchaseOrder createPurchaseOrder(
      User buyerUser,
      Company company,
      Partner contactPartner,
      Currency currency,
      LocalDate deliveryDate,
      String internalReference,
      String externalReference,
      LocalDate orderDate,
      PriceList priceList,
      Partner supplierPartner,
      TradingName tradingName)
      throws D3worksException;

  String getSequence(Company company) throws D3worksException;

  public void setDraftSequence(PurchaseOrder purchaseOrder) throws D3worksException;

  @Transactional
  public Partner validateSupplier(PurchaseOrder purchaseOrder);

  public void savePurchaseOrderPDFAsAttachment(PurchaseOrder purchaseOrder) throws D3worksException;

  public void requestPurchaseOrder(PurchaseOrder purchaseOrder) throws D3worksException;

  public PurchaseOrder mergePurchaseOrders(
      List<PurchaseOrder> purchaseOrderList,
      Currency currency,
      Partner supplierPartner,
      Company company,
      Partner contactPartner,
      PriceList priceList,
      TradingName tradingName)
      throws D3worksException;

  public void updateCostPrice(PurchaseOrder purchaseOrder) throws D3worksException;
}
